import {delay, toast} from "../../utils/tool";

const app = getApp()

Page({
    data: {
        inputValue: '',
    },
    onLoad: function (options) {

    },
    inputHandle(e) {
        this.setData({
            inputValue: e.detail.value
        })
    },
    toRoomPage(data,codeValue) {
        toast({
            title: '获取成功'
        }, () => {
            wx.navigateTo({
                url: '/pages/validate-rooms/validate-rooms',
                success: ev => {
                    ev.eventChannel.emit('getData', {data,codeValue})
                }
            })
        })
    },
    commitHandle() {
        const {globalData: {firstStore}} = app
        if (!firstStore) return toast({title: '请选择店铺', icon: 'error'})
        const {inputValue} = this.data
        if (!inputValue.length) return toast({title: '请输入验券码'})
        wx.showLoading({title: '正在验券', mask: true})
        app.ajax({
            url: '/api/user/yanquanindex',
            method: 'POST',
            data: {
                qr_code: inputValue,
                store_id: firstStore.id,
            },
            success: async resp => {
                await delay(300)
                wx.hideLoading()
                const {code, data, msg} = resp
                if (code !== 1) return toast({title: msg, time: 2000})
                if (!data || !data.length) return wx.showModal({
                    title: '错误!',
                    content: '未匹配到包间数据。',
                    showCancel: false,
                    confirmText: '确定'
                })
                this.toRoomPage(data,inputValue)
            }
        })
    },
    scanCode: function () {
        wx.scanCode({
            success: res => {
              console.log(res,'resp')
                this.setData({
                    inputValue: res.result
                })
            }
        })
    },
});




